raw_data = File.read(File.join(File.dirname(__FILE__), 'ahadith.txt'))
table_rows = raw_data.split(/[\r\n]/).grep(/^\|\|/)

ahadith = []

table_rows.each do |table_row|
  parts = table_row.split(/\s*\|\|\s*/)[1..-1]
  hadith_no = parts[0].scan(/\d+/).first.to_i
  hadith = {}
  case parts[0]
    when /^\d+$/
      hadith[:arabic] = parts[1]
      hadith[:sanad] = parts[2]
    when /^e\d+$/
      hadith[:english] = parts[1]
    when /^c\d+$/
      hadith[:comment] = parts[1]
  end
  (ahadith[hadith_no] ||= {}).update(hadith) unless hadith.empty?
end

ahadith.each_with_index do |hadith, index|
  if hadith
    File.open(File.join(File.dirname(__FILE__), "..", "res", "ahadith", "#{index}"), "w") do |f|
      f << hadith[:arabic] << "\r\n"
      f << hadith[:sanad] << "\r\n"
      f << hadith[:english] << "\r\n"
      f << hadith[:comment]
    end
  end
end

